home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / add-log.el < prev    next >
Lisp/Scheme  |  1993-11-23  |  17KB  |  455 lines

  1. ;;; add-log.el --- change log maintenance commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1988, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: maint
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; This facility is documented in the Emacs Manual.
  26.  
  27. ;;; Code:
  28.  
  29. (defvar change-log-default-name nil
  30.   "*Name of a change log file for \\[add-change-log-entry].")
  31.  
  32. (defvar add-log-current-defun-function nil
  33.   "\
  34. *If non-nil, function to guess name of current function from surrounding text.
  35. \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
  36. instead) with no arguments.  It returns a string or nil if it cannot guess.")
  37.  
  38. ;; This MUST not be autoloaded, since user-login-name
  39. ;; cannot be known at Emacs dump time.
  40. (defvar add-log-full-name (user-full-name)
  41.   "*Full name of user, for inclusion in ChangeLog daily headers.
  42. This defaults to the value returned by the `user-full-name' function.")
  43.  
  44. ;; This MUST not be autoloaded, since user-login-name
  45. ;; cannot be known at Emacs dump time.
  46. (defvar add-log-mailing-address (concat (user-login-name) "@" (system-name))
  47.   "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
  48. This defaults to the value returned by `user-login-name', followed by
  49. an `@' character, followed by the value returned by `system-name'.")
  50.  
  51. (defun change-log-name ()
  52.   (or change-log-default-name
  53.       (if (eq system-type 'vax-vms) "$CHANGE_LOG$.TXT" "ChangeLog")))
  54.  
  55. ;;;###autoload
  56. (defun prompt-for-change-log-name ()
  57.   "Prompt for a change log name."
  58.   (let ((default (change-log-name)))
  59.     (expand-file-name
  60.      (read-file-name (format "Log file (default %s): " default)
  61.              nil default))))
  62.  
  63. ;;;###autoload
  64. (defun find-change-log (&optional file-name)
  65.   "Find a change log file for \\[add-change-log-entry] and return the name.
  66. Optional arg FILE-NAME is a name to try first.
  67. If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
  68. Failing that, use \"ChangeLog\" in the current directory.
  69. If the file does not exist in the named directory, successive parent
  70. directories are tried.
  71.  
  72. Once a file is found, `change-log-default-name' is set locally in the
  73. current buffer to the complete file name."
  74.   (or file-name
  75.       (setq file-name (or change-log-default-name
  76.               ;; Chase links in the source file
  77.               ;; and use the change log in the dir where it points.
  78.               (and buffer-file-name
  79.                    (file-name-directory
  80.                 (file-chase-links buffer-file-name)))
  81.               default-directory)))
  82.   (if (and (eq file-name change-log-default-name)
  83.        (assq 'change-log-default-name (buffer-local-variables)))
  84.       ;; Don't do the searching if we already have a buffer-local value.
  85.       file-name
  86.  
  87.     (if (file-directory-p file-name)
  88.     (setq file-name (expand-file-name (change-log-name) file-name)))
  89.     ;; Chase links before visiting the file.
  90.     ;; This makes it easier to use a single change log file
  91.     ;; for several related directories.
  92.     (setq file-name (file-chase-links file-name))
  93.     (setq file-name (expand-file-name file-name))
  94.     ;; Move up in the dir hierarchy till we find a change log file.
  95.     (let ((file1 file-name)
  96.       parent-dir)
  97.       (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
  98.           (progn (setq parent-dir
  99.                    (file-name-directory
  100.                 (directory-file-name
  101.                  (file-name-directory file1))))
  102.              ;; Give up if we are already at the root dir.
  103.              (not (string= (file-name-directory file1)
  104.                        parent-dir))))
  105.     ;; Move up to the parent dir and try again.
  106.     (setq file1 (expand-file-name 
  107.              (file-name-nondirectory (change-log-name))
  108.          parent-dir)))
  109.       ;; If we found a change log in a parent, use that.
  110.       (if (or (get-file-buffer file1) (file-exists-p file1))
  111.       (setq file-name file1)))
  112.     ;; Make a local variable in this buffer so we needn't search again.
  113.     (set (make-local-variable 'change-log-default-name) file-name)
  114.     file-name))
  115.  
  116. ;;;###autoload
  117. (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
  118.   "Find change log file and add an entry for today.
  119. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  120. Second arg is file name of change log.  If nil, uses `change-log-default-name'.
  121. Third arg OTHER-WINDOW non-nil means visit in other window.
  122. Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
  123. never append to an existing entry."
  124.   (interactive (list current-prefix-arg
  125.              (prompt-for-change-log-name)))
  126.   (if whoami
  127.       (progn
  128.     (setq add-log-full-name (read-input "Full name: " add-log-full-name))
  129.      ;; Note that some sites have room and phone number fields in
  130.      ;; full name which look silly when inserted.  Rather than do
  131.      ;; anything about that here, let user give prefix argument so that
  132.      ;; s/he can edit the full name field in prompter if s/he wants.
  133.     (setq add-log-mailing-address
  134.           (read-input "Mailing address: " add-log-mailing-address))))
  135.   (let ((defun (funcall (or add-log-current-defun-function
  136.                 'add-log-current-defun)))
  137.     paragraph-end entry)
  138.  
  139.     (setq file-name (find-change-log file-name))
  140.  
  141.     ;; Set ENTRY to the file name to use in the new entry.
  142.     (and buffer-file-name
  143.      ;; Never want to add a change log entry for the ChangeLog file itself.
  144.      (not (string= buffer-file-name file-name))
  145.      (setq entry (if (string-match
  146.               (concat "^" (regexp-quote (file-name-directory
  147.                              file-name)))
  148.               buffer-file-name)
  149.              (substring buffer-file-name (match-end 0))
  150.                (file-name-nondirectory buffer-file-name))))
  151.  
  152.     (if (and other-window (not (equal file-name buffer-file-name)))
  153.     (find-file-other-window file-name)
  154.       (find-file file-name))
  155.     (or (eq major-mode 'change-log-mode)
  156.     (change-log-mode))
  157.     (undo-boundary)
  158.     (goto-char (point-min))
  159.     (if (looking-at (concat (regexp-quote (substring (current-time-string)
  160.                              0 10))
  161.                 ".* " (regexp-quote add-log-full-name)
  162.                 "  (" (regexp-quote add-log-mailing-address)))
  163.     (forward-line 1)
  164.       (insert (current-time-string)
  165.           "  " add-log-full-name
  166.           "  (" add-log-mailing-address ")\n\n"))
  167.  
  168.     ;; Search only within the first paragraph.
  169.     (if (looking-at "\n*[^\n* \t]")
  170.     (skip-chars-forward "\n")
  171.       (forward-paragraph 1))
  172.     (setq paragraph-end (point))
  173.     (goto-char (point-min))
  174.  
  175.     ;; Now insert the new line for this entry.
  176.     (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
  177.        ;; Put this file name into the existing empty entry.
  178.        (if entry
  179.            (insert entry)))
  180.       ((and (not new-entry)
  181.         (re-search-forward
  182.          (concat (regexp-quote (concat "* " entry))
  183.              ;; Don't accept `foo.bar' when
  184.              ;; looking for `foo':
  185.              "\\(\\s \\|[(),:]\\)")
  186.          paragraph-end t))
  187.        ;; Add to the existing entry for the same file.
  188.        (re-search-forward "^\\s *$\\|^\\s \\*")
  189.        (beginning-of-line)
  190.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  191.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  192.        (insert "\n\n")
  193.        (forward-line -2)
  194.        (indent-relative-maybe))
  195.       (t
  196.        ;; Make a new entry.
  197.        (forward-line 1)
  198.        (while (looking-at "\\sW")
  199.          (forward-line 1))
  200.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  201.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  202.        (insert "\n\n\n")
  203.        (forward-line -2)
  204.        (indent-to left-margin)
  205.        (insert "* " (or entry ""))))
  206.     ;; Now insert the function name, if we have one.
  207.     ;; Point is at the entry for this file,
  208.     ;; either at the end of the line or at the first blank line.
  209.     (if defun
  210.     (progn
  211.       ;; Make it easy to get rid of the function name.
  212.       (undo-boundary)
  213.       (insert (if (save-excursion
  214.             (beginning-of-line 1)
  215.             (looking-at "\\s *$")) 
  216.               ""
  217.             " ")
  218.           "(" defun "): "))
  219.       ;; No function name, so put in a colon unless we have just a star.
  220.       (if (not (save-excursion
  221.          (beginning-of-line 1)
  222.          (looking-at "\\s *\\(\\*\\s *\\)?$")))
  223.       (insert ": ")))))
  224.  
  225. ;;;###autoload
  226. (defun add-change-log-entry-other-window (&optional whoami file-name)
  227.   "Find change log file in other window and add an entry for today.
  228. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  229. Second arg is file name of change log.  \
  230. If nil, uses `change-log-default-name'."
  231.   (interactive (if current-prefix-arg
  232.            (list current-prefix-arg
  233.              (prompt-for-change-log-name))))
  234.   (add-change-log-entry whoami file-name t))
  235. ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
  236.  
  237. ;;;###autoload
  238. (defun change-log-mode ()
  239.   "Major mode for editing change logs; like Indented Text Mode.
  240. Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
  241. New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
  242. Each entry behaves as a paragraph, and the entries for one day as a page.
  243. Runs `change-log-mode-hook'."
  244.   (interactive)
  245.   (kill-all-local-variables)
  246.   (indented-text-mode)
  247.   (setq major-mode 'change-log-mode
  248.     mode-name "Change Log"
  249.     left-margin 8
  250.     fill-column 74)
  251.   (use-local-map change-log-mode-map)
  252.   ;; Let each entry behave as one paragraph:
  253.   (set (make-local-variable 'paragraph-start) "^\\s *$\\|^\f")
  254.   (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^\f\\|^\\sw")
  255.   ;; Let all entries for one day behave as one page.
  256.   ;; Match null string on the date-line so that the date-line
  257.   ;; is grouped with what follows.
  258.   (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
  259.   (set (make-local-variable 'version-control) 'never)
  260.   (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
  261.   (run-hooks 'change-log-mode-hook))
  262.  
  263. (defvar change-log-mode-map nil
  264.   "Keymap for Change Log major mode.")
  265. (if change-log-mode-map
  266.     nil
  267.   (setq change-log-mode-map (make-sparse-keymap))
  268.   (define-key change-log-mode-map "\M-q" 'change-log-fill-paragraph))
  269.  
  270. ;; It might be nice to have a general feature to replace this.  The idea I
  271. ;; have is a variable giving a regexp matching text which should not be
  272. ;; moved from bol by filling.  change-log-mode would set this to "^\\s *\\s(".
  273. ;; But I don't feel up to implementing that today.
  274. (defun change-log-fill-paragraph (&optional justify)
  275.   "Fill the paragraph, but preserve open parentheses at beginning of lines.
  276. Prefix arg means justify as well."
  277.   (interactive "P")
  278.   (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
  279.     (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
  280.     (fill-paragraph justify)))
  281.  
  282. (defvar add-log-current-defun-header-regexp
  283.   "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[a-z_---A-Z]+\\)[ \t]*[:=]"
  284.   "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
  285.  
  286. (defun add-log-current-defun ()
  287.   "Return name of function definition point is in, or nil.
  288.  
  289. Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
  290. Texinfo (@node titles), and Fortran.
  291.  
  292. Other modes are handled by a heuristic that looks in the 10K before
  293. point for uppercase headings starting in the first column or
  294. identifiers followed by `:' or `=', see variable
  295. `add-log-current-defun-header-regexp'.
  296.  
  297. Has a preference of looking backwards."
  298.   (condition-case nil
  299.       (save-excursion
  300.     (let ((location (point)))
  301.       (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode))
  302.          ;; If we are now precisely a the beginning of a defun,
  303.          ;; make sure beginning-of-defun finds that one
  304.          ;; rather than the previous one.
  305.          (or (eobp) (forward-char 1))
  306.          (beginning-of-defun)
  307.          ;; Make sure we are really inside the defun found, not after it.
  308.          (if (and (progn (end-of-defun)
  309.                  (< location (point)))
  310.               (progn (forward-sexp -1)
  311.                  (>= location (point))))
  312.              (progn
  313.                (if (looking-at "\\s(")
  314.                (forward-char 1))
  315.                (forward-sexp 1)
  316.                (skip-chars-forward " ")
  317.                (buffer-substring (point)
  318.                      (progn (forward-sexp 1) (point))))))
  319.         ((and (memq major-mode '(c-mode 'c++-mode))
  320.               (save-excursion (beginning-of-line)
  321.                       ;; Use eq instead of = here to avoid
  322.                       ;; error when at bob and char-after
  323.                       ;; returns nil.
  324.                       (while (eq (char-after (- (point) 2)) ?\\)
  325.                     (forward-line -1))
  326.                       (looking-at "[ \t]*#[ \t]*define[ \t]")))
  327.          ;; Handle a C macro definition.
  328.          (beginning-of-line)
  329.          (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
  330.            (forward-line -1))
  331.          (search-forward "define")
  332.          (skip-chars-forward " \t")
  333.          (buffer-substring (point)
  334.                    (progn (forward-sexp 1) (point))))
  335.         ((memq major-mode '(c-mode 'c++-mode))
  336.          (beginning-of-line)
  337.          ;; See if we are in the beginning part of a function,
  338.          ;; before the open brace.  If so, advance forward.
  339.          (while (not (looking-at "{\\|\\(\\s *$\\)"))
  340.            (forward-line 1))
  341.          (or (eobp)
  342.              (forward-char 1))
  343.          (beginning-of-defun)
  344.          (if (progn (end-of-defun)
  345.                 (< location (point)))
  346.              (progn
  347.                (backward-sexp 1)
  348.                (let (beg tem)
  349.  
  350.              (forward-line -1)
  351.              ;; Skip back over typedefs of arglist.
  352.              (while (and (not (bobp))
  353.                      (looking-at "[ \t\n]"))
  354.                (forward-line -1))
  355.              ;; See if this is using the DEFUN macro used in Emacs,
  356.              ;; or the DEFUN macro used by the C library.
  357.              (if (condition-case nil
  358.                  (and (save-excursion
  359.                     (forward-line 1)
  360.                     (backward-sexp 1)
  361.                     (beginning-of-line)
  362.                     (setq tem (point))
  363.                     (looking-at "DEFUN\\b"))
  364.                       (>= location tem))
  365.                    (error nil))
  366.                  (progn
  367.                    (goto-char tem)
  368.                    (down-list 1)
  369.                    (if (= (char-after (point)) ?\")
  370.                    (progn
  371.                      (forward-sexp 1)
  372.                      (skip-chars-forward " ,")))
  373.                    (buffer-substring (point)
  374.                          (progn (forward-sexp 1) (point))))
  375.                ;; Ordinary C function syntax.
  376.                (setq beg (point))
  377.                (if (condition-case nil
  378.                    ;; Protect against "Unbalanced parens" error.
  379.                    (progn
  380.                      (down-list 1) ; into arglist
  381.                      (backward-up-list 1)
  382.                      (skip-chars-backward " \t")
  383.                      t)
  384.                  (error nil))
  385.                    ;; Verify initial pos was after
  386.                    ;; real start of function.
  387.                    (if (and (save-excursion
  388.                       (goto-char beg)
  389.                       ;; For this purpose, include the line
  390.                       ;; that has the decl keywords.  This
  391.                       ;; may also include some of the
  392.                       ;; comments before the function.
  393.                       (while (and (not (bobp))
  394.                               (save-excursion
  395.                             (forward-line -1)
  396.                             (looking-at "[^\n\f]")))
  397.                         (forward-line -1))
  398.                       (>= location (point)))
  399.                     ;; Consistency check: going down and up
  400.                     ;; shouldn't take us back before BEG.
  401.                     (> (point) beg))
  402.                    (buffer-substring (point)
  403.                              (progn (backward-sexp 1)
  404.                                 (point))))))))))
  405.         ((memq major-mode
  406.                '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
  407.                   plain-tex-mode latex-mode;; cmutex.el
  408.                   ))
  409.          (if (re-search-backward
  410.               "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
  411.              (progn
  412.                (goto-char (match-beginning 0))
  413.                (buffer-substring (1+ (point));; without initial backslash
  414.                      (progn
  415.                        (end-of-line)
  416.                        (point))))))
  417.         ((eq major-mode 'texinfo-mode)
  418.          (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
  419.              (buffer-substring (match-beginning 1)
  420.                        (match-end 1))))
  421.                 ((eq major-mode 'fortran-mode)
  422.                  ;; must be inside function body for this to work
  423.                  (beginning-of-fortran-subprogram)
  424.                  (let ((case-fold-search t)) ; case-insensitive
  425.                    ;; search for fortran subprogram start
  426.                    (if (re-search-forward
  427.              "^[ \t]*\\(program\\|subroutine\\|function\
  428. \\|[ \ta-z0-9*]*[ \t]+function\\)"
  429.              nil t)
  430.                        (progn
  431.                          ;; move to EOL or before first left paren
  432.                          (if (re-search-forward "[(\n]" nil t)
  433.                  (progn (forward-char -1)
  434.                     (skip-chars-backward " \t"))
  435.                (end-of-line))
  436.              ;; Use the name preceding that.
  437.                          (buffer-substring (point)
  438.                                            (progn (forward-sexp -1)
  439.                                                   (point)))))))
  440.         (t
  441.          ;; If all else fails, try heuristics
  442.          (let (case-fold-search)
  443.            (end-of-line)
  444.            (if (re-search-backward add-log-current-defun-header-regexp
  445.                        (- (point) 10000)
  446.                        t)
  447.                (buffer-substring (match-beginning 1)
  448.                      (match-end 1))))))))
  449.     (error nil)))
  450.  
  451.  
  452. (provide 'add-log)
  453.  
  454. ;;; add-log.el ends here
  455.